/*     */ package com.zimbra.cs.util.yauth;
/*     */ 
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.util.HashMap;
/*     */ import org.apache.commons.httpclient.HttpException;
/*     */ import org.apache.commons.httpclient.methods.GetMethod;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TokenAuthenticateV1
/*     */ {
/*     */   private String mCrumb;
/*     */   private String mY;
/*     */   private String mT;
/*     */   
/*     */   public static String getToken(String username, String passwd)
/*     */     throws IOException, HttpException
/*     */   {
/*  51 */     GetMethod method = new GetMethod("https://login.yahoo.com/config/pwtoken_get?src=ymsgr&login=" + username + "&passwd=" + passwd);
/*  52 */     int response = HttpClientUtil.executeMethod(method);
/*     */     
/*  54 */     if ((response >= 200) && (response < 300)) {
/*  55 */       String body = method.getResponseBodyAsString();
/*     */       
/*  57 */       HashMap<String, String> map = new HashMap();
/*  58 */       map.put("ymsgr", null);
/*     */       
/*  60 */       parseResponseBody(body, map);
/*     */       
/*  62 */       return (String)map.get("ymsgr");
/*     */     }
/*  64 */     throw new IOException("HTTPClient response: " + response);
/*     */   }
/*     */   
/*     */   private static void parseResponseBody(String responseBody, HashMap<String, String> map)
/*     */   {
/*  69 */     String[] lines = responseBody.split("\n");
/*  70 */     for (String line : lines) {
/*  71 */       int eqIdx = line.indexOf('=');
/*  72 */       if (eqIdx > 0) {
/*  73 */         String[] cols = new String[2];
/*  74 */         line = line.trim();
/*  75 */         cols[0] = line.substring(0, eqIdx);
/*  76 */         cols[1] = "";
/*  77 */         if (eqIdx < line.length() - 1) {
/*  78 */           cols[1] = line.substring(eqIdx + 1);
/*  79 */           if ((map.containsKey(cols[0])) && (map.get(cols[0]) == null))
/*     */           {
/*  81 */             map.put(cols[0], cols[1]);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public static TokenAuthenticateV1 doAuth(String username, String token)
/*     */     throws IOException, HttpException
/*     */   {
/*  94 */     GetMethod method = new GetMethod("https://login.yahoo.com/config/pwtoken_login?src=ymsgr&token=" + token);
/*  95 */     int response = HttpClientUtil.executeMethod(method);
/*     */     
/*  97 */     if ((response >= 200) && (response < 300)) {
/*  98 */       String body = method.getResponseBodyAsString();
/*     */       
/* 100 */       HashMap<String, String> map = new HashMap();
/* 101 */       map.put("crumb", null);
/* 102 */       map.put("Y", null);
/* 103 */       map.put("T", null);
/*     */       
/* 105 */       parseResponseBody(body, map);
/*     */       
/* 107 */       return new TokenAuthenticateV1((String)map.get("crumb"), (String)map.get("Y"), (String)map.get("T"));
/*     */     }
/* 109 */     throw new IOException("HTTPClient response: " + response);
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 114 */     return "YToken(crumb=" + this.mCrumb + ",Y=" + this.mY + ",T=" + this.mT + ")";
/*     */   }
/*     */   
/*     */   private TokenAuthenticateV1(String crumb, String Y, String T) {
/* 118 */     if ((crumb == null) || (Y == null) || (T == null)) {
/* 119 */       throw new IllegalArgumentException("Missing part of auth response");
/*     */     }
/* 121 */     this.mCrumb = crumb;
/* 122 */     this.mY = Y;
/* 123 */     this.mT = T;
/*     */   }
/*     */   
/* 126 */   public String getCrumb() { return this.mCrumb; }
/* 127 */   public String getY() { return this.mY; }
/* 128 */   public String getT() { return this.mT; }
/*     */   
/*     */ 
/*     */ 
/*     */   public static void main(String[] argv)
/*     */   {
/*     */     try
/*     */     {
/* 136 */       String ymsgr = getToken("XXXX", "XXXX");
/* 137 */       System.out.println("Got token: " + ymsgr);
/*     */       
/* 139 */       TokenAuthenticateV1 ta = doAuth("XXXX", ymsgr);
/*     */       
/* 141 */       System.out.println("Got token: " + ta.toString());
/*     */     } catch (Exception e) {
/* 143 */       System.out.println("Caught exception" + e);
/* 144 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/util/yauth/TokenAuthenticateV1.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */